package com.dolittle.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

import android.content.Context;

public class WordList {

	private HashSet<Word> mhs;
	private ArrayList<Word> mList;
	private String fileAdress = "/sdcard/MyList/list.txt";
	private Context mContext;

	public WordList(Context context) {
		mContext = context;
		mhs = new HashSet<Word>();
		mList = new ArrayList<Word>();
		read(fileAdress);
	}

	private void read(String file) {
		InputStream inputStream;
		try {
			inputStream = new BufferedInputStream(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			inputStream = mContext.getResources()
					.openRawResource((R.raw.words));
		}
		readFile(inputStream);
		// Collections.sort(mList);
		writeFile();
	}

	private void readFile(InputStream inputStream) {
		if (inputStream != null) {
			InputStreamReader inputreader = new InputStreamReader(inputStream);
			BufferedReader buffreader = new BufferedReader(inputreader);
			String swedish;
			String spanish;
			try {
				while ((swedish = buffreader.readLine()) != null) {
					spanish = buffreader.readLine();
					Word word = new Word(swedish, spanish);
					mhs.add(word);
					mList.add(word);
				}
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public String toString() {
		if (mList.isEmpty()) {
			return "List is empty!";
		}
		StringBuilder sb = new StringBuilder();
		for (Word w : mList) {
			sb.append(w.toString() + "\n");
		}
		return sb.toString();
	}

	@SuppressWarnings("unchecked")
	public void writeFile() {
		Collections.sort(mList);
		try {
			FileWriter writer = new FileWriter(fileAdress);
			if (writer != null) {
				for (Word w : mList) {
					writer.write(w.write());
				}
			}
			writer.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

	@SuppressWarnings("unchecked")
	public boolean addWord(String swedish, String spanish) {
		if (swedish.length() < 1 || spanish.length() < 1) {
			return false;
		}
		Word word = new Word(swedish, spanish);
		boolean added = false;
		if (!mhs.contains(word)) {
			mhs.add(word);
			mList.add(word);
			Collections.sort(mList);
			added = true;
		}
		return added;
	}
	
	@SuppressWarnings("unchecked")
	public boolean addWord(Word word) {
		if (word.getSwedish().length() < 1 || word.getSpanish().length() < 1) {
			return false;
		}
		boolean added = false;
		if (!mhs.contains(word)) {
			mhs.add(word);
			mList.add(word);
			Collections.sort(mList);
			added = true;
		}
		return added;
	}

	public boolean editWord(String fromSwedish, String fromSpanish,
			String toSwedish, String toSpanish) {
		if (removeWord(fromSwedish, fromSpanish)) {
			return addWord(toSwedish, toSpanish);
		}
		return false;
	}
	
	public boolean editWord(Word from, Word to) {
		if (removeWord(from)) {
			return addWord(to);
		}
		return false;
	}

	public boolean removeWord(String swedish, String spanish) {
		Word w = new Word(swedish, spanish);
		if (mhs.remove(w)) {
			mList.remove(w);
			return true;
		}
		return false;
	}
	public boolean removeWord(Word word) {
		if (mhs.remove(word)) {
			mList.remove(word);
			return true;
		}
		return false;
	}

	public ArrayList<Word> getList() {
		return mList;
	}
}
